<?php
class fs_tables extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model("fs_tree");
    }

    /* Table list */
    function get_tables($db_name)
    {
        $results = array();
        $query = $this->db->query('SHOW TABLES FROM ' . $this->db->escape_str($db_name));

        foreach ($query->result_array() as $row)
        {
           array_push($results, $row['Tables_in_' . $this->db->escape_str($db_name)]);
        }

        return $results;
    }

    /* Table data with meta */
    function get_table_data($table)
    {
        $results = array();
        $dbtmp = $this->select_single_db();

        $fields = $dbtmp->field_data($table);

        foreach ($fields as $field)
        {
           array_push($results, $field);
        }

        return $results;
    }

    /* Tmp fix to access this single db */
    function select_single_db()
    {
        $config['hostname'] = "localhost";
        $config['username'] = "root";
        $config['password'] = "root";
        $config['database'] = $this->session->userdata('active_site');
        $config['dbdriver'] = "mysql";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";

        return $this->load->database($config, true);
    }

    /* Table list with first records */
    function get_tree_with_records($db_name, $number_of_records)
    {
        $results = array();

        $dbtmp = $this->select_single_db();
        $query = $dbtmp->query('SHOW TABLES FROM ' . $this->db->escape_str($db_name));

        foreach ($query->result_array() as $row)
        {
           // Table
           $table_name = $row['Tables_in_' . $this->db->escape_str($db_name)];

           $ident_field = $this->get_field_by_type($table_name, "int");
           $char_field = $this->get_field_by_type($table_name, "string");
           $records = $dbtmp->query('SELECT '.$ident_field.' AS ID, '.$char_field.' AS NAME, \''.$table_name.'\' as ACTIVE_TABLE FROM ' . $this->db->escape_str($table_name) . ' ORDER BY '. $ident_field .' DESC LIMIT '.$number_of_records);

           array_push($results, array("name"        =>  $table_name, "url" => $this->fs_tree->tree_table_url($table_name),
                                      "records"     =>  $this->fs_tree->tree_get_record_html($records)
                      ));
        }
        return $results;
    }

    /* Generate overview table */
    function generate_grid_table($table, $width)
    {
        $results = array();
        $dbtmp = $this->select_single_db();
        $grid_offset = 108;

        $fields = $dbtmp->list_fields($table);
        $width =  round(($width-$grid_offset) / count($fields));

        // Database heading
        foreach ($fields as $field)
        {
           array_push($results, array(
                                  "name" => $field,
                                  "width" => $width,
                                  "align" => "left",
                                  "sortable" => "true",
                                  "label" => $field,
                                ));
        }


        return $results;
    }

    function get_field_by_type($table, $type)
    {
        $dbtmp = $this->select_single_db();
        $fields = $dbtmp->field_data($table);

        //print_r($fields);
        foreach ($fields as $field)
        {
            if($type == $field->type) {
                return $field->name;
                break;
            }
        }
    }
}